package com.paranoid.flashcard.logic;

import android.os.AsyncTask;

import com.paranoid.flashcard.dao.FlashcardDao;
import com.paranoid.flashcard.model.Card;
import com.paranoid.flashcard.model.Deck;

public class StudySession {
    FlashcardDao mFlashcardDao;
	private final FirstToLastSequence sequence;
	
	public StudySession(Deck deck, FlashcardDao flashcardDao) {
		sequence = new FirstToLastSequence(deck);
		mFlashcardDao = flashcardDao;
	}
	
	public FlashCardViewModel getNext() {
		return sequence.getNext();
	}
	
	public void righAnswerForActiveCard() {
		Card card = sequence.getActiveCard();
		card.incMasteryLevel();
		new UpdateMasteryTask().execute(card);
	}
	
	public void wrongAnswerForActiveCard() {
		Card card = sequence.getActiveCard();
		card.resetMasteryLevel();
	}
	
	
	private class UpdateMasteryTask extends AsyncTask<Card, Void, Void> {
        protected Void doInBackground(Card... params) {
            if (params.length < 1)
                return null;
            
            for (Card card : params) {
                mFlashcardDao.updateCardMasteryLevel(card.getId(), card.getMasteryLevel());
            }
            
            return null;
        }
	    
	}
}
